import { BaseAction, Schema } from '../../BaseAction'
import { ActionName } from '../../types'

interface Payload {
  status: number | string
  ext_status: number | string
  battery_status: number | string
}

export class SetOnlineStatus extends BaseAction<Payload, null> {
  actionName = ActionName.SetOnlineStatus
  payloadSchema = Schema.object({
    status: Schema.union([Number, String]).required(),
    ext_status: Schema.union([Number, String]).required(),
    battery_status: Schema.union([Number, String]).required()
  })

  async _handle(payload: Payload) {
    const ret = await this.ctx.ntUserApi.setSelfStatus(
      Number(payload.status),
      Number(payload.ext_status),
      Number(payload.battery_status),
    )
    if (ret.result !== 0) {
      throw new Error(ret.errMsg)
    }
    return null
  }
}
